package com.chinaisafe.user.controller;

import com.chinaisafe.base.common.validated.UpdateGroup;
import com.chinaisafe.user.service.UserLoginService;
import com.chinaisafe.user.vo.request.LoginTenantReqVO;
import com.chinaisafe.user.vo.response.LoginRespVO;
import com.chinaisafe.user.vo.response.UserInfoRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.chinaisafe.base.common.annotation.OperationLog;
import com.chinaisafe.base.common.enums.BusinessTypeEnum;
import com.chinaisafe.base.common.vo.Answer;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.chinaisafe.user.vo.request.SysUserReqVO;
import com.chinaisafe.user.vo.response.SysUserRespVO;
import org.springframework.validation.annotation.Validated;
import com.chinaisafe.user.service.SysUserService;
import org.springframework.web.bind.annotation.RestController;
import com.chinaisafe.base.common.validated.AddGroup;

/**
 * SysUserController
 * Description：用户接口层
 *
 * @author xuefu.shu
 * @date  2024-07-12 09:55:54
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户接口层", description = "用户接口层")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Resource
    private UserLoginService userLoginService;

    @GetMapping("/pageList")
    @Operation(summary = "获取用户分页列表", description = "获取用户分页列表")
    public Answer<IPage<SysUserRespVO>> pageList(SysUserReqVO sysUserReqVO) {
        IPage<SysUserRespVO> pageList = sysUserService.getPageList(sysUserReqVO);
        return Answer.success("获取成功", pageList);
    }

    @PostMapping()
    @Operation(summary = "新增用户", description = "新增用户")
    @OperationLog(title = "用户-新增", businessType = BusinessTypeEnum.INSERT)
    public Answer<SysUserRespVO> add(@RequestBody @Validated(AddGroup.class) SysUserReqVO sysUserReqVO) {
        SysUserRespVO sysUserRespVO = sysUserService.add(sysUserReqVO);
        return Answer.success("保存成功", sysUserRespVO);
    }

    @PutMapping()
    @Operation(summary = "根据id编辑用户", description = "根据id编辑用户")
    @OperationLog(title = "用户-根据id编辑", businessType = BusinessTypeEnum.UPDATE)
    public Answer<SysUserRespVO> update(@RequestBody @Validated(UpdateGroup.class) SysUserReqVO sysUserReqVO) {
        SysUserRespVO sysUserRespVO = sysUserService.update(sysUserReqVO);
        return Answer.success("修改成功", sysUserRespVO);
    }

    @GetMapping("/{sysUserId}")
    @Operation(summary = "根据id获取用户", description = "根据id获取用户")
    public Answer<SysUserRespVO> getDetailById(@PathVariable Long sysUserId) {
        SysUserRespVO sysUserRespVO = sysUserService.getDetailById(sysUserId);
        return Answer.success("获取成功", sysUserRespVO);
    }

    @DeleteMapping()
    @Operation(summary = "根据id删除用户", description = "根据id删除用户")
    @OperationLog(title = "用户-根据id删除", businessType = BusinessTypeEnum.DELETE)
    public Answer<Void> deleteByIds(@RequestParam("ids") Long[] ids) {
        sysUserService.deleteByIds(ids);
        return Answer.success("删除成功");
    }


    @PostMapping("/login/tenant")
    @Operation(summary = "用户切换租户", description = "用户切换租户")
    @OperationLog(title = "用户切换租户", businessType = BusinessTypeEnum.GRANT)
    public Answer<LoginRespVO> loginTenant(@RequestBody @Validated LoginTenantReqVO tenantReqVO) {
        return Answer.successWithData(userLoginService.loginTenant(tenantReqVO));
    }

    @GetMapping("/info")
    @Operation(summary = "获取用户信息", description = "获取用户信息")
    public Answer<UserInfoRespVO> userInfo() {
        return Answer.successWithData(userLoginService.getUserInfo());
    }

}

